<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

use app\admin\model\Manager;
use app\admin\model\Student;
use app\admin\model\Instructor;
use app\admin\model\Qiandao;
use app\admin\model\TopicRes;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        // $this->user_id = 7;
    } 

     /**
     * 角色登录(返回role_id用于话题提交)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/login)
     * @ApiParams   (name="type",type=string,required=true,description="角色选择，请传英文过来:manager=管理员,student=学生,instructor=辅导员")
     * @ApiParams   (name="xuehao",type=string,required=false,description="学号")
     * @ApiParams   (name="name",type=string,required=false,description="名称,三个角色都有这个字段的，都传过来进行了")
     * @ApiParams   (name="job",type=string,required=false,description="管理员职务")
     * @ApiParams   (name="gonghao",type=string,required=false,description="管理员工号")
     * @ApiParams   (name="banji",type=string,required=false,description="学生班级")
     * @ApiParams   (name="xuexiao",type=string,required=false,description="学生学校")
     * @ApiParams   (name="yuanxi",type=string,required=false,description="学生院系")
     * @ApiParams   (name="phone",type=int,required=false,description="辅导员手机号码")
     * @ApiReturn   ({
        {
          "code": 1,
          "msg": "登录成功",
          "time": "1591173622",
          "data": "2"
        }
      })
     */
    public function login(){

        $params = input();
        $params['user_id'] = $this->user_id;

        switch ($params['type']) {
          case 'manager':

            $manager =  new Manager($params);
            $res = $manager->allowField(true)->save();
            $role_id = $manager->id;
            # code...
            break;

          case 'student':
            $student =  new Student($params);
            $res = $student->allowField(true)->save();
            $role_id = $student->id;
            # code...
            break;
          
          default:
            $instructor =  new Instructor($params);
            $res = $instructor->allowField(true)->save();
            $role_id = $instructor->id;
            # code...
            break;
        }

        if($res)$this->success('登录成功',$role_id);
    } 

    /**
     * 不同角色获取不同的通知
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/get_notic)
     * @ApiParams   (name="type",type=string,required=true,description="角色选择，请传英文过来:manager=管理员,student=学生,instructor=辅导员")
     * @ApiReturn   ({
    {
      "code": 1,
      "msg": "通知列表",
      "time": "1591173786",
      "data": [
        {
          "time": "2020-06-03 16:04:04",
          "title": "测试2",
          "content": "通知给管理员",
          "id": 4
        },
        {
          "time": "2020-06-03 15:03:29",
          "title": "测试",
          "content": "通知给管理员",
          "id": 1
        }
      ]
    }
    })
    
     */
    public function get_notic(){
       
        $params = input();
        $type = $params['type'];
        $list = Db::query("select FROM_UNIXTIME( updatetime, '%Y-%m-%d %H:%I:%S' ) as time ,title,content,id from fa_notic where type = '$type' order by updatetime desc");
        $this->success('通知列表',$list);
    }

    /**
     * 打卡和获取是否打卡
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/qiandao)
     * @ApiParams   (name="status",type=string,required=true,description="传1过来表示打卡，传0或者不传表示查看今日打卡状态")
     * @ApiReturn   ({
      // 查看打卡情况
      {
        "code": 1,
        "msg": "今日已打卡",
        "time": "1591173877",
        "data": null
      }
      // 或者
      {
        "code": 0,
        "msg": "今日未打卡",
        "time": "1591173928",
        "data": null
      }
      // 查看打卡情况

      // 打卡操作
      {
        "code": 1,
        "msg": "打卡成功",
        "time": "1591173952",
        "data": null
      }

      })
    
     */
    public function qiandao(){
        $params = input();
        if($params['status']==1){
          $params['user_id'] = $this->user_id;
          $Qiandao = new Qiandao($params);
          $res = $Qiandao->allowField(true)->save();
          if($res)$this->success('打卡成功');
        }else{
          $my_qiandao = Db::query("select * from `fa_qiandao` Where user_id = $this->user_id and date_format(from_unixtime(updatetime),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') ");
          if(empty($my_qiandao[0])){
            $this->error('今日未打卡');
          }else{
            $this->success('今日已打卡');
          }
        }
    }

    /**
     * 话题列表
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/topic_list)
     * @ApiReturn   ({
         {
          "code": 1,
          "msg": "话题列表",
          "time": "1591174020",
          "data": [
            {
              "id": 2,
              "name": "如果可以给十年前的自己打一通电话，你会？",
              "updatetime": 1591174017
            },
            {
              "id": 1,
              "name": "如何评价特朗普？",
              "updatetime": 1591172024
            }
          ]
        }
      })
    
     */
    public function topic_list(){
      $topic_list = Db::name('topic')->order('id desc')->select();
      $this->success('话题列表',$topic_list);
      
    }

    /**
     * 话题留言评论
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/topic_comment)
     * @ApiParams   (name="content",type=string,required=true,description="留言内容")
     * @ApiParams   (name="topic_id",type=int,required=true,description="话题id")
     * @ApiReturn   ({
  
        })
    
     */
    public function topic_comment(){
      $params = input();
      $params['user_id'] = $this->user_id;

      $TopicRes = new TopicRes($params);
      $res = $TopicRes->allowField(true)->save();
      if($res)$this->success('留言成功');
    }

     
    

  

}
